package com.aidong.myprofile.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.activity.biz.MstCampaignBiz;
import com.aidong.activity.entity.Member;
import com.aidong.activity.entity.MemberSportTags;
import com.aidong.myprofile.biz.TrnAttentionBiz;
import com.aidong.myprofile.entity.TrnAttention;
import com.aidong.util.CountDistance;
import com.aidong.util.PageUtil;
import com.aidong.util.ReadPropertiesUtil;
import com.aidong.util.SortUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 好友关注
 * @author java
 * @version 1.0    
 *  2015.07.05
 */
@ParentPackage("json")
@Namespace("/")
public class TrnAttentionAction implements ModelDriven<TrnAttention> {

	private TrnAttention attention;
	
	@Override
	public TrnAttention getModel() {
		if(attention == null){
			attention = new TrnAttention();
		}
		return attention;
	}
	
	private TrnAttentionBiz trnAttentionBiz; // 关注service
	
	private MstCampaignBiz mstCampaignBiz;
	
	private Map<String, Object> map;
	
	private String coordinate; // 经纬度
	
	private int pageCurrent; // 当前页

	public void setPageCurrent(int pageCurrent) {
		this.pageCurrent = pageCurrent;
	}

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}

	public void setCoordinate(String coordinate) {
		this.coordinate = coordinate;
	}

	/**
	 * 关注service
	 * @param trnAttentionBiz
	 */
	@Autowired
	public void setTrnAttentionBiz(TrnAttentionBiz trnAttentionBiz) {
		this.trnAttentionBiz = trnAttentionBiz;
	}
	
	@Autowired
	public void setMstCampaignBiz(MstCampaignBiz mstCampaignBiz) {
		this.mstCampaignBiz = mstCampaignBiz;
	}
	
	/**
	 * 加关注
	 * @return
	 */
	@Action(value="addtrnattention",results={@Result(name="succ",type="json",params={"root","map"})})
	public String addTrnAttention(){
		map = new HashMap<String, Object>();
		try {
			int idongId = attention.getIdongId(); // 关注人
			int attentionIdongId = attention.getAttentionIdongId(); // 被关注人
			if(idongId > 0 && attentionIdongId > 0){
				int count = Integer.parseInt(trnAttentionBiz.getIfAttentionStatus(attention));
				if(count > 0){
					map.put("code", 0);
					map.put("msg", "他(她)已经被你关注了");
					return "succ";
				}else{
					trnAttentionBiz.addTrnAttention(attention);
					map.put("code", 1);
					map.put("result", "关注成功");
					return "succ";
				}
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 取消关注
	 * @return
	 */
	@Action(value="deltrnattention",results={@Result(name="succ",type="json",params={"root","map"})})
	public String delTrnAttention(){
		map = new HashMap<String, Object>();
		try {
			int idongId = attention.getIdongId(); // 关注人
			int attentionIdongId = attention.getAttentionIdongId(); // 被关注人
			if(idongId > 0 && attentionIdongId > 0){
				int status = Integer.parseInt(trnAttentionBiz.getIfAttentionStatus(attention));
				if(status > 0){
					trnAttentionBiz.delTrnAttention(attention);
					map.put("code", 1);
					map.put("result", "取消成功");
					return "succ";
				}else{
					map.put("code", 1);
					map.put("msg", "你还未关注过他(她)");
					return "succ";
				}
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 我的关注列表、粉丝列表
	 * @return
	 */
	@Action(value="getattentionlist",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getMyAttentionList(){
		map = new HashMap<String, Object>();
		try {
			int idongId = attention.getIdongId(); // idongId(相对人 关注状态)
			
			int focusId = attention.getFocusId(); // 关注列表
			
			int fansId = attention.getFansId(); // 粉丝列表
			
			List<Member> attentions = new ArrayList<Member>();
			
		    Map<String, Object> pageMap = PageUtil.page(20, pageCurrent);
			
			if(focusId > 0){ // 关注列表
				pageMap.put("focusId", focusId);
				attentions = trnAttentionBiz.getFocusList(pageMap);
			}
			if(fansId > 0){ // 粉丝列表
				pageMap.put("fansId", fansId);
				attentions = trnAttentionBiz.getFansList(pageMap);
			}
			if(attentions.size() > 0){
				for (Member member : attentions) {
					if(member != null){
						String headUrl = member.getHeadUrl(); // 头像
						if(!"".equals(headUrl) && headUrl != null){
							member.setHeadUrl(ReadPropertiesUtil.getIMGREAD(headUrl));
						}else{
							member.setHeadUrl("");
						}
						
						List<MemberSportTags> tags = member.getSportTags();
						if(tags.size() > 0){
							for (MemberSportTags tag : tags) {
								String tagUrl = tag.getSportTagsUrl();
								if(!"".equals(tagUrl) && tagUrl != null){
									tag.setSportTagsUrl(ReadPropertiesUtil.getIMGREAD(tag.getSportTagsUrl()));
								}else{
									tag.setSportTagsUrl("");
								}
							}
						}
						
						double myLon = member.getLon(); // 经度
						double myLat = member.getLat(); // 纬度
						
						if((!"".equals(coordinate) && coordinate != null)
								&& myLon > 0 && myLat > 0){
							String[] reqLaction = coordinate.split(",");
							if(reqLaction.length > 0){
								double discance = CountDistance.distance(
										Double.parseDouble(reqLaction[0]), 
										Double.parseDouble(reqLaction[1]),
										myLon,myLat);
								int dis = (int) Math.rint(discance); // 四舍五入取整数
								member.setDistance(dis);
								int count = dis / 1000;
								if(count < 1){
									if(dis <= 100){
										member.setCoordinate("<100m"); // 相距距离
									}else if(100 < dis && dis <= 500){
										member.setCoordinate("<500m"); // 相距距离
									}else if(500 < dis && dis <= 800){
										member.setCoordinate("<800m"); // 相距距离
									}else{
										member.setCoordinate(dis + " m"); // 相距距离
									}
								}else{
									member.setCoordinate(count + " km"); // 相距距离
								}
							}
						}else{
							member.setDistance(0);
							member.setCoordinate(""); // 相距距离
						}
						
						// 关注状态
						if(idongId > 0){
							Map<String, Object> paraMapAtten = new HashMap<String, Object>();
							paraMapAtten.put("idongId", idongId); // 关注人
							paraMapAtten.put("tionIdongId", member.getIdongId()); // 被关注人
							int attionCount = mstCampaignBiz.getIfAttention(paraMapAtten);		
							if(attionCount > 0){ // 已关注
								member.setAttionStatus("1");
							}else{ // 关注
								member.setAttionStatus("2");
							}
						}else{
							member.setAttionStatus("2");
						}
					}
				}
			}
			
			// 按距离排序
			if(attentions.size() > 0){
				SortUtil.attionbySort(attentions);
			}
			
			map.put("code", 1);
			map.put("result", attentions);
			map.put("pageCurrent", pageMap.get("pageCurrent"));
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
}
